<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Home\RegisterEmailRequest;
use App\Http\Requests\Home\RegisterPhoneRequest;
use App\Http\Requests\Home\ForgetPhoneRequest;
use App\Http\Requests\Home\ForgetEmailRequest;
use Hash;
use Mail;
use App\Models\User;
use App\Models\Userinfo;

class RegisterController extends Controller
{
    //加载注册页面
    public function getIndex()
    {
        return view('home.register');
    }

    //ajax验证邮箱是否存在
    public function getAjax(Request $request)
    {
        $email = $request['email'];
        $res = User::where('uemail',$email)->first();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    //邮箱注册
    public function postIndex(RegisterEmailRequest $request)
    {
        $data = $request->except('_token','urepassword');

        $token = str_random(50);
        $user = new User;
        $data['uname'] = $data['uemail'];
        $data['remember_token'] = $token;
        $data['upassword'] = Hash::make($data['upassword']);

        $uid = $user->insertGetId($data);
        if($uid){
            self::getEmail($data['uemail'],$uid,$token);
            echo '注册邮件已发至邮箱,激活后可登录';
        }else{
            echo '失败';
        }
    }

    //发送激活邮件
    static public function getEmail($uemail,$uid,$token)
    {
        Mail::send('home.mail',['uid'=>$uid,'token'=>$token],function($m) use ($uemail){
            $m->to($uemail)->subject('Mall 激活邮件!');
        });
    }

    //激活
    public function getJihuo(Request $request)
    {
        $data = $request->all();
        $user = User::find($data['uid']);
        if($user->remember_token != $data['token']){
            return "<script>alert('验证失败,请重新注册');location.href='/home/register/index'</script>";
        }
        $user->ustatus = 1;
        $user->remember_token = str_random(50);
        if($user->save()){
            $userinfo = new Userinfo;
            $userinfo->uf_uid = $data['uid'];
            $userinfo->save();
            return "<script>alert('激活成功,请登录');location.href='/home/login/index'</script>";
        }else{
            return '激活失败';
        }
    }

    //发送手机验证码
    public function getPhonecode(Request $request){
        return 2;
        $phone = $request->input('phone');
        //检验是否重复发送
        $exists = \Redis::exists('STRING:PHONE:CODE:'.$phone);
        if(!empty($exists)){
            return 1;
        }
        $code = rand(1000,9999);
        $url = 'http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C03435536&password=758eae53a6da145d32f8e026850cd5b3&format=json&mobile='.$phone.'&content=您的验证码是：'.$code.'。请不要把验证码泄露给其他人。';
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        // 将json转化为数组
        $arr = json_decode($res,true);
        //$arr['code']==2 代表发送成功
        if($arr['code']==2){
            //防止重复发送
            \Redis::setex('STRING:PHONE:CODE:'.$phone,60,$code);
            return 2;
        }else{
            return 0;
        }
    }

    //ajax验证手机号是否存在
    public function getAjaxphone(Request $request)
    {
        $phone = $request['uphone'];
        $res = User::where('uphone',$phone)->first();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    //手机号注册
    public function postPhone(RegisterPhoneRequest $request){
        $data = $request->except('_token','urepassword');
        $phone = $data['uphone'];
        $phonecode = \Redis::get('STRING:PHONE:CODE:'.$phone);
        if($data['uphonecode']!=$phonecode){
            return back()->with('errors','验证码错误')->withinput();
        }
        $token = str_random(50);
        $user = new User;
        $user->uname = $data['uphone'];
        $user->uphone = $data['uphone'];
        $user->remember_token = $token;
        $user->upassword = Hash::make($data['upassword']);
        $user->ustatus = 1;
        if($user->save()){
            $userinfo = new Userinfo;
            $userinfo->uf_uid = $data['uid'];
            $userinfo->save();
            return "<script>alert('注册成功,请登录');location.href='/home/login'</script>";
        }else{
            return "<script>alert('注册失败,请重试');location.href='/home/register/index'</script>";
        }
    }

    //忘记密码，邮箱找回页面
    public function getForget()
    {
        return view('home.forget');
    }

    //向要充值的用户邮箱发送验证邮箱
    public function postForget(Request $request)
    {
        $data = $request->all();
        $user = User::where('uemail',$data['uemail'])->first();
        $uemail = $user['uemail'];
        $token = $user['remember_token'];
        self::getRemail($uemail,$user['uid'],$token);
        return '验证邮件已发至邮箱,重置密码后可登录';
    }

    //发送验证邮箱
    static public function getRemail($uemail,$uid,$token)
    {
        Mail::send('home.mailforget',['uemail'=>$uemail,'token'=>$token],function($m) use ($uemail){
            $m->to($uemail)->subject('Mall 密码找回邮件!');
        });
    }

    //加载邮箱重置密码页面
    public function getResetemail(Request $request)
    {
        $uemail = $request->uemail;
        $token = $request->token;
        return view('home.resetemail',['uemail'=>$uemail,'token'=>$token]);
    }

    //邮箱重置密码
    public function postResetemail(ForgetEmailRequest $request)
    {
        $uemail = $request->uemail;
        $token = $request->token;
        $user = User::where('uemail',$uemail)->first();
        if($user->remember_token != $token){
            return "<script>alert('验证失败');location.href='/home/register/forget'</script>";
        }
        $user->remember_token = str_random(50);
        $user->upassword = Hash::make($request->upassword);
        if($user->save()){
            return "<script>alert('密码找回成功,请登录');location.href='/home/login'</script>";
        }else{
            return "<script>alert('密码找回失败,请重试');location.href='/home/register/forget'</script>";
        }
    }

    //加载手机重置密码页面
    public function getForgetphone()
    {
        return view('home.forgetphone');
    }

    //手机重置密码
    public function postForgetphone(ForgetPhoneRequest $request)
    {
        $data = $request->except('_token','urepassword');
        if($data['phonecode']!=session('phonecode')){
            return back()->with('errors','验证码错误')->withinput();
        }
        $token = str_random(50);
        $user = User::where('uphone',$data['uphone'])->first();
        $user->remember_token = $token;
        $user->upassword = Hash::make($data['upassword']);
        if($user->save()){
            return "<script>alert('密码找回成功,请登录');location.href='/home/login'</script>";
        }else{
            return "<script>alert('密码找回失败,请重试');location.href='".$_SERVER['HTPP_REFERER']."'</script>";
        }
    }

}
